package com.finger.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.finger.commonutils.JwtUtils;
import com.finger.commonutils.R;
import com.finger.eduservice.client.OrderClient;
import com.finger.eduservice.entity.EduCourse;
import com.finger.eduservice.entity.EduTeacher;
import com.finger.eduservice.entity.frontvo.CourseWebVo;
import com.finger.eduservice.entity.vo.ChapterVO;
import com.finger.eduservice.service.EduChapterService;
import com.finger.eduservice.service.EduCourseService;
import com.finger.eduservice.service.EduTeacherService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/indexfront")
@CrossOrigin
public class IndexFrontController {
    @Autowired
    private EduCourseService eduCourseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduTeacherService eduTeacherService;

    @Autowired
    private OrderClient orderClient;

    @GetMapping("/getCourseListAndTeacherList")
    public R getCourseListAndTeacherList(){


        List<EduCourse> eduList = eduCourseService.selectIndexCourseList();


        List<EduTeacher> teacherList = eduTeacherService.selectIndexTeacherList();

        return R.ok().data("eduList",eduList).data("teacherList",teacherList);
    }

    @GetMapping("/getTeacherList/{page}/{size}")
    public R getTeacherList(@PathVariable(required = false) long page,
                            @PathVariable(required = false) long size){
        Page<EduTeacher> eduTeacherPage = new Page<>(page, size);

        Map<String,Object> map = eduTeacherService.selectTeacherPageList(eduTeacherPage);

        return R.ok().data("page",map);
    }
    @GetMapping("/getTeacherInfo/{id}")
    public R getTeacherInfo(@PathVariable String id){

        List<EduCourse> courses = eduCourseService.getCourseInfoByTeacherId(id);

        EduTeacher teacher = eduTeacherService.getById(id);

        return R.ok().data("courseList",courses).data("teacher",teacher);
    }

    @GetMapping("/getFrontCourseInfoVO/{courseId}")
    public R getFrontCourseInfoVO(@PathVariable String courseId,
                                  HttpServletRequest req){

        CourseWebVo courseWebVo = eduCourseService.getCourseWebVoById(courseId);

        List<ChapterVO> allChapter = eduChapterService.getAllChapter(courseId);

        String memberId = JwtUtils.getMemberIdByJwtToken(req);
        if(!StringUtils.isEmpty(memberId)){
            boolean isBuy = orderClient.isBuy(courseId, memberId);

            // 价格不足1 强转为int会造成精度损失，字符串不会
            String price = courseWebVo.getPrice().toString();

            // 如果价格不为0 并且已购买
            if(isBuy && !"0.00".equals(price)){
                // 如果查询到购买 就把价格设置为0
                courseWebVo.setPrice(new BigDecimal(0));
            }
        }

        return R.ok().data("allChapterList",allChapter).data("courseWebVo",courseWebVo);
    }

}
